<?php
class SEO
{
    static function newSubStr($string ,$limit)
    {
        if(strlen($string) <= $limit)
        {
            return $string;
        }
        else
        {
            if(strpos($string," ",$limit) > $limit)
            {
                $newLimit=strpos($string," ",$limit);
                $newString = substr($string,0,$newLimit)."...";
                return $newString;
            }
            $newString = substr($string,0,$limit)."...";
            return $newString;
        }
    }
    static function escapeTitle($str)
    {
        $str = str_replace("\""," ",$str);
        $str = str_replace("'"," ",$str);
        $str = preg_replace('!\s+!', ' ', $str);
        return $str;
    }

    static function friendly_url($str) {
        $str = preg_replace("/(à|á|ạ|ả|ã|â|ầ|ấ|ậ|ẩ|ẫ|ă|ằ|ắ|ặ|ẳ|ẵ)/", 'a' , $str);
        $str = preg_replace("/(è|é|ẹ|ẻ|ẽ|ê|ề|ế|ệ|ể|ễ)/", 'e', $str);
        $str = preg_replace("/(ì|í|ị|ỉ|ĩ)/", 'i', $str);
        $str = preg_replace("/(ò|ó|ọ|ỏ|õ|ô|ồ|ố|ộ|ổ|ỗ|ơ|ờ|ớ|ợ|ở|ỡ)/", 'o', $str);
        $str = preg_replace("/(ù|ú|ụ|ủ|ũ|ư|ừ|ứ|ự|ử|ữ)/", 'u', $str);
        $str = preg_replace("/(ỳ|ý|ỵ|ỷ|ỹ)/", 'y', $str);
        $str = preg_replace("/(đ)/", 'd', $str);
        $str = preg_replace("/(À|Á|Ạ|Ả|Ã|Â|Ầ|Ấ|Ậ|Ẩ|Ẫ|Ă|Ằ|Ắ|Ặ|Ẳ|Ẵ)/", 'A', $str);
        $str = preg_replace("/(È|É|Ẹ|Ẻ|Ẽ|Ê|Ề|Ế|Ệ|Ể|Ễ)/", 'E', $str);
        $str = preg_replace("/(Ì|Í|Ị|Ỉ|Ĩ)/", 'I', $str);
        $str = preg_replace("/(Ò|Ó|Ọ|Ỏ|Õ|Ô|Ồ|Ố|Ộ|Ổ|Ỗ|Ơ|Ờ|Ớ|Ợ|Ở|Ỡ)/", 'O', $str);
        $str = preg_replace("/(Ù|Ú|Ụ|Ủ|Ũ|Ư|Ừ|Ứ|Ự|Ử|Ữ)/", 'U', $str);
        $str = preg_replace("/(Ỳ|Ý|Ỵ|Ỷ|Ỹ)/", 'Y', $str);
        $str = preg_replace("/(Đ)/", 'D', $str);
        setlocale(LC_ALL, '');
        $str = @iconv('UTF-8', 'ASCII//TRANSLIT', $str);
        $str = str_replace("-"," ",$str);
        $str = str_replace("_"," ",$str);
        $str = str_replace("|"," ",$str);
        $str = str_replace("\""," ",$str);
        $str = preg_replace('!\s+!', ' ', $str);
        //$str = str_replace(" ", "-", str_replace("&*#39;","",$str));
        //$url = preg_replace('~[^\\pL0-9_]+~u', '-', $str);
        $str = strtolower($str);
        $str = trim ($str);
        $str = preg_replace('~[^-a-z0-9_]+~', '-', $str);

        //$str = str_replace(" ", "-", str_replace("&*#39;","",$str));
        return $str;
    }
    
	static function friendly_url_image($str) {
        $str = preg_replace("/(à|á|ạ|ả|ã|â|ầ|ấ|ậ|ẩ|ẫ|ă|ằ|ắ|ặ|ẳ|ẵ)/", 'a' , $str);
        $str = preg_replace("/(è|é|ẹ|ẻ|ẽ|ê|ề|ế|ệ|ể|ễ)/", 'e', $str);
        $str = preg_replace("/(ì|í|ị|ỉ|ĩ)/", 'i', $str);
        $str = preg_replace("/(ò|ó|ọ|ỏ|õ|ô|ồ|ố|ộ|ổ|ỗ|ơ|ờ|ớ|ợ|ở|ỡ)/", 'o', $str);
        $str = preg_replace("/(ù|ú|ụ|ủ|ũ|ư|ừ|ứ|ự|ử|ữ)/", 'u', $str);
        $str = preg_replace("/(ỳ|ý|ỵ|ỷ|ỹ)/", 'y', $str);
        $str = preg_replace("/(đ)/", 'd', $str);
        $str = preg_replace("/(À|Á|Ạ|Ả|Ã|Â|Ầ|Ấ|Ậ|Ẩ|Ẫ|Ă|Ằ|Ắ|Ặ|Ẳ|Ẵ)/", 'A', $str);
        $str = preg_replace("/(È|É|Ẹ|Ẻ|Ẽ|Ê|Ề|Ế|Ệ|Ể|Ễ)/", 'E', $str);
        $str = preg_replace("/(Ì|Í|Ị|Ỉ|Ĩ)/", 'I', $str);
        $str = preg_replace("/(Ò|Ó|Ọ|Ỏ|Õ|Ô|Ồ|Ố|Ộ|Ổ|Ỗ|Ơ|Ờ|Ớ|Ợ|Ở|Ỡ)/", 'O', $str);
        $str = preg_replace("/(Ù|Ú|Ụ|Ủ|Ũ|Ư|Ừ|Ứ|Ự|Ử|Ữ)/", 'U', $str);
        $str = preg_replace("/(Ỳ|Ý|Ỵ|Ỷ|Ỹ)/", 'Y', $str);
        $str = preg_replace("/(Đ)/", 'D', $str);
        setlocale(LC_ALL, '');
        $str = @iconv('UTF-8', 'ASCII//TRANSLIT', $str);
        $str = str_replace("-"," ",$str);
        $str = str_replace("_"," ",$str);
        $str = str_replace("|"," ",$str);
        $str = str_replace("\""," ",$str);
        $str = preg_replace('!\s+!', ' ', $str);
        //$str = str_replace(" ", "-", str_replace("&*#39;","",$str));
        //$url = preg_replace('~[^\\pL0-9_]+~u', '-', $str);
        $str = strtolower($str);
        $str = trim ($str);
        $str = preg_replace('~[^-a-z0-9_]+~', '', $str);

        //$str = str_replace(" ", "-", str_replace("&*#39;","",$str));
        return $str;
    }

    static function removeVietNameseChar($str) {
        $str = preg_replace("/(à|á|ạ|ả|ã|â|ầ|ấ|ậ|ẩ|ẫ|ă|ằ|ắ|ặ|ẳ|ẵ)/", 'a' , $str);
        $str = preg_replace("/(è|é|ẹ|ẻ|ẽ|ê|ề|ế|ệ|ể|ễ)/", 'e', $str);
        $str = preg_replace("/(ì|í|ị|ỉ|ĩ)/", 'i', $str);
        $str = preg_replace("/(ò|ó|ọ|ỏ|õ|ô|ồ|ố|ộ|ổ|ỗ|ơ|ờ|ớ|ợ|ở|ỡ)/", 'o', $str);
        $str = preg_replace("/(ù|ú|ụ|ủ|ũ|ư|ừ|ứ|ự|ử|ữ)/", 'u', $str);
        $str = preg_replace("/(ỳ|ý|ỵ|ỷ|ỹ)/", 'y', $str);
        $str = preg_replace("/(đ)/", 'd', $str);
        $str = preg_replace("/(À|Á|Ạ|Ả|Ã|Â|Ầ|Ấ|Ậ|Ẩ|Ẫ|Ă|Ằ|Ắ|Ặ|Ẳ|Ẵ)/", 'A', $str);
        $str = preg_replace("/(È|É|Ẹ|Ẻ|Ẽ|Ê|Ề|Ế|Ệ|Ể|Ễ)/", 'E', $str);
        $str = preg_replace("/(Ì|Í|Ị|Ỉ|Ĩ)/", 'I', $str);
        $str = preg_replace("/(Ò|Ó|Ọ|Ỏ|Õ|Ô|Ồ|Ố|Ộ|Ổ|Ỗ|Ơ|Ờ|Ớ|Ợ|Ở|Ỡ)/", 'O', $str);
        $str = preg_replace("/(Ù|Ú|Ụ|Ủ|Ũ|Ư|Ừ|Ứ|Ự|Ử|Ữ)/", 'U', $str);
        $str = preg_replace("/(Ỳ|Ý|Ỵ|Ỷ|Ỹ)/", 'Y', $str);
        $str = preg_replace("/(Đ)/", 'D', $str);
        $str = str_replace("\"","",$str);
        $str = trim ($str);
        return $str;
    }

    static function getSEOContent (){
        $seo = array();

        $title = 'Thành Phố Game - Thiên Đường Giải Trí';
        $metaDescription = 'Thành Phố Game - Nơi game thủ tìm thấy niềm vui đích thực. Gamer có thể tải và chơi mọi game mobi online và offline hot nhất trên thị trường hiện nay.';
        $metaKeyword = 'tai game mobi, tai game cho di dong, tai game mobi online, choi game mobi, game mobi, hot nhat';

        $context = new BaseContext();
        $serviceResult = new ServiceResult();

        $gameCategoryService = new GameCategoryService();
        $gamePromotionService = new GamePromotionService();
        $appService = new AppService();

        $gameService = new GameService();
        if(isset($_GET['func']) && $_GET['func'] == 'detailGame')
        {
            $id = $_GET['id'];
            $gameArr = $gameService->getGameByID($context, $serviceResult, $id);
            $title = $gameArr['name'] . ' - ' . $gameArr['mediumName'];
            $title = SEO::escapeTitle($title);
            $metaDescription = $gameArr['SEO_metaDescription'];
            $metaKeyword = $gameArr['SEO_keyword'];
            $metaDescription = SEO::escapeTitle($metaDescription);
            $image = WEB_IMAGE_URL . '/' . $gameArr['largeImage'];

            $facebook_tag = "<meta property=\"og:image\" content=\"".$image."\" />\n";
            $facebook_tag .= "<meta property=\"og:title\" content=\"".$title."\" />\n";
            $facebook_tag .= "<meta property=\"og:description\" content=\"".$metaDescription."\" />\n";
        }
        else if(isset($_GET['func']) && $_GET['func'] == 'listGame')
        {
            $id = $_GET['id'];
            $gameArr = $gameCategoryService->getGameCategoryById($context, $serviceResult, $id);
            $title = 'Game ' . $gameArr['name'] . ' - ' . 'Tải Game ' . SEO::removeVietNameseChar($gameArr['name']);
            $title = SEO::escapeTitle($title);
            $metaDescription = $gameArr['SEO_metaDescription'];
            $metaDescription = SEO::escapeTitle($metaDescription);
            $metaKeyword = $gameArr['SEO_keyword'];
            $image = WEB_IMAGE_URL . '/' . $gameArr['largeImage'];

            $facebook_tag = "<meta property=\"og:image\" content=\"".$image."\" />\n";
            $facebook_tag .= "<meta property=\"og:title\" content=\"".$title."\" />\n";
            $facebook_tag .= "<meta property=\"og:description\" content=\"".$metaDescription."\" />\n";
        }
        else if(isset($_GET['func']) && ($_GET['func'] == 'detailPromotion' || $_GET['func'] == 'detailGuide'))
        {
            $id = $_GET['id'];
            $content = $gamePromotionService->getGamePromotionByID($context, $serviceResult, $id);
            $title = $content['title'];
            $title = SEO::escapeTitle($title);
            $metaDescription = $content['SEO_metaDescription'];
            $metaDescription = SEO::escapeTitle($metaDescription);
            $metaKeyword = $content['SEO_keyword'];
        }
        else if(isset($_GET['func']) && ($_GET['func'] == 'listPromotion' || $_GET['func'] == 'listGuide'))
        {
            if($_GET['func'] == 'listPromotion')
            {
                $gameCategory = $gameCategoryService->getGameCategoryById($context, $serviceResult, 6);
            }
            else
            {
                $gameCategory = $gameCategoryService->getGameCategoryById($context, $serviceResult, 5);
            }
            $title = $gameCategory['SEO_title'];
            $title = SEO::escapeTitle($title);
            $metaDescription = $gameCategory['SEO_metaDescription'];
            $metaDescription = SEO::escapeTitle($metaDescription);
            $metaKeyword = $gameCategory['SEO_keyword'];
        }
        else if(isset($_GET['func']) && $_GET['func'] == 'detailApp')
        {
            $id = $_GET['id'];
            $app = $appService->getAppById($context, $serviceResult, $id);

            $title = $app['name'] . ' - ' . $app['mediumName'];
            $title = SEO::escapeTitle($title);
            $metaDescription = $app['SEO_metaDescription'];
            $metaDescription = SEO::escapeTitle($metaDescription);
            $metaKeyword = $app['SEO_keyword'];
        }
        else if(isset($_GET['func']) && ($_GET['func'] == 'listApp' || $_GET['func'] == 'listTuVi'))
        {
            if($_GET['func'] == 'listApp')
            {
                $gameCategory = $gameCategoryService->getGameCategoryById($context, $serviceResult, 8);
            }
            else
            {
                $gameCategory = $gameCategoryService->getGameCategoryById($context, $serviceResult, 7);
            }
            $title = $gameCategory['SEO_title'];
            $title = SEO::escapeTitle($title);
            $metaDescription = $gameCategory['SEO_metaDescription'];
            $metaDescription = SEO::escapeTitle($metaDescription);
            $metaKeyword = $gameCategory['SEO_keyword'];
        }
        else if(isset($_GET['func']) && $_GET['func'] == 'error')
        {

        }
        else{
            $title = SEO::escapeTitle($title);
            $metaDescription = SEO::escapeTitle($metaDescription);
            $facebook_tag = "<meta property=\"og:image\" content=\"http://thanhphogame.com/view/images/thanhphogame.png\" />\n";
            $facebook_tag .= "<meta property=\"og:title\" content=\"".$title."\" />\n";
            $facebook_tag .= "<meta property=\"og:description\" content=\"".$metaDescription."\" />\n";
        }

        $seo['title'] = SEO::escapeTitle($title);
        $seo['metaDescription'] = SEO::escapeTitle($metaDescription);
        $seo['metaKeyword'] = $metaKeyword;
        $seo['facebook_tag'] = $facebook_tag;
        return $seo;
    }

    static function getSEODublin (){
        $seo = array();
        $dubin_tag  = '<link rel="schema.DC" href="http://purl.org/dc/elements/1.1/">' . "\n";

        $dubin_tag .= '<meta name="DC.Creator" content="ThanhPhoGame.com">' . "\n";

        $dubin_tag .= '<meta name="DC.Publisher" content="ThanhPhoGame.com">' . "\n";

        $dubin_tag .= '<meta name="DC.Type" content="Image">' . "\n";
        $dubin_tag .= '<meta name="DC.Type" content="Text">' . "\n";
        $dubin_tag .= '<meta name="DC.Type" content="Game">' . "\n";

        $dubin_tag .= '<meta name="DC.Language" content="vi">' . "\n";

        $context = new BaseContext();
        $serviceResult = new ServiceResult();


        $gameCategoryService = new GameCategoryService();
        $gamePromotionService = new GamePromotionService();
        $appService = new AppService();

        $gameService = new GameService();
        if(isset($_GET['func']) && $_GET['func'] == 'detailGame')
        {
            $id = $_GET['id'];
            $game = $gameService->getGameByID($context, $serviceResult, $id);
            $gameDetailLink = WEB_BASE_URL . '/' . 'detail-game-' . $game['id'] . '/' . SEO::friendly_url($game['name'] . '-' . $game['mediumName']) .'.html';

            $dubin_tag .= '<meta name="DC.Subject" content="' . SEO::escapeTitle($game['name'] . ' - ' . $game['mediumName']) .'">' . "\n";
            $dubin_tag .= '<meta name="DC.Description" content="'.SEO::escapeTitle($game['SEO_metaDescription']).'">' . "\n";
            $dubin_tag .= '<meta name="DC.Title" content="' . SEO::escapeTitle($game['name'] . ' - ' . $game['mediumName']) .'">' . "\n";
            $dubin_tag .= '<link rel="DC.Source" href="'. SEO::escapeTitle($gameDetailLink).'">' . "\n";

        }
        else if(isset($_GET['func']) && $_GET['func'] == 'listGame')
        {
            $id = $_GET['id'];
            $gameCategory = $gameCategoryService->getGameCategoryById($context, $serviceResult, $id);
            $title = 'Game ' . $gameCategory['name'] . ' - ' . 'Tải Game ' . $gameCategory['name'];
            $gameCategoryLink = WEB_BASE_URL . '/' . 'list-game-' . $gameCategory['id'] . '/' . SEO::friendly_url($gameCategory['name']) . '.html';

            $dubin_tag .= '<meta name="DC.Subject" content="' . SEO::escapeTitle($title) .'">' . "\n";
            $dubin_tag .= '<meta name="DC.Description" content="'.$gameCategory['SEO_metaDescription'].'">' . "\n";
            $dubin_tag .= '<meta name="DC.Title" content="' . SEO::escapeTitle($title) .'">' . "\n";
            $dubin_tag .= '<link rel="DC.Source" href="'.$gameCategoryLink.'">' . "\n";
        }
        else if(isset($_GET['func']) && ($_GET['func'] == 'detailPromotion' || $_GET['func'] == 'detailGuide'))
        {
            $id = $_GET['id'];
            $content = $gamePromotionService->getGamePromotionByID($context, $serviceResult, $id);
            $title = $content['title'];

        }
        else if(isset($_GET['func']) && ($_GET['func'] == 'listPromotion' || $_GET['func'] == 'listGuide'))
        {
            if($_GET['func'] == 'listPromotion')
            {
                $gameCategory = $gameCategoryService->getGameCategoryById($context, $serviceResult, 6);
            }
            else
            {
                $gameCategory = $gameCategoryService->getGameCategoryById($context, $serviceResult, 5);
            }
            $title = $gameCategory['SEO_title'];
            $metaDescription = $gameCategory['SEO_metaDescription'];
            $metaKeyword = $gameCategory['SEO_keyword'];
        }
        else if(isset($_GET['func']) && $_GET['func'] == 'detailApp')
        {
            $id = $_GET['id'];
            $app = $appService->getAppById($context, $serviceResult, $id);

            $title = $app['name'] . ' - ' . $app['mediumName'];
            $metaDescription = $app['SEO_metaDescription'];
            $metaKeyword = $app['SEO_keyword'];
        }
        else if(isset($_GET['func']) && ($_GET['func'] == 'listApp' || $_GET['func'] == 'listTuVi'))
        {
            if($_GET['func'] == 'listApp')
            {
                $gameCategory = $gameCategoryService->getGameCategoryById($context, $serviceResult, 8);
            }
            else
            {
                $gameCategory = $gameCategoryService->getGameCategoryById($context, $serviceResult, 7);
            }
            $title = $gameCategory['SEO_title'];
            $metaDescription = $gameCategory['SEO_metaDescription'];
            $metaKeyword = $gameCategory['SEO_keyword'];
        }
        else if(isset($_GET['func']) && $_GET['func'] == 'error')
        {

        }
        else
        {
            $title = 'Thành Phố Game - Thiên Đường Giải Trí';
            $metaDescription = 'Thành Phố Game - Nơi game thủ tìm thấy niềm vui đích thực. Gamer có thể tải và chơi mọi game mobi online và offline hot nhất trên thị trường hiện nay.';
            $metaKeyword = 'tai game mobi, tai game cho di dong, tai game mobi online, choi game mobi, game mobi, hot nhat';

            $dubin_tag .= '<meta name="DC.Subject" content="Thành Phố Game - Thiên Đường Giải Trí">' . "\n";
            $dubin_tag .= '<meta name="DC.Description" content="Thành Phố Game - Nơi game thủ tìm thấy niềm vui đích thực. Gamer có thể tải và chơi mọi game mobi online và offline hot nhất trên thị trường hiện nay.">' . "\n";
            $dubin_tag .= '<meta name="DC.Title" content="Thành Phố Game - Thiên Đường Giải Trí">' . "\n";
            $dubin_tag .= '<link rel="DC.Source" href="'. WEB_BASE_URL .'">' . "\n";
        }

        return $dubin_tag;
    }

    /* takes an input, scrubs unnecessary words */
    function remove_words($input,$replace,$words_array = array(),$unique_words = true)
    {
        //separate all words based on spaces
        $input_array = explode(' ',$input);

        //create the return array
        $return = array();

        //loops through words, remove bad words, keep good ones
        foreach($input_array as $word)
        {
            //if it's a word we should add...
            if(!in_array($word,$words_array) && ($unique_words ? !in_array($word,$return) : true))
            {
                $return[] = $word;
            }
        }

        //return good words separated by dashes
        return implode($replace,$return);
    }

    function dateDiff($date, $numDate)
    {
        $dateNow = time();
        $convertDate = new DateTime($date);
        $dateTime = $convertDate->getTimestamp();
        $dateDiff = $dateNow - $dateTime;
        $rs = $dateDiff/3600/24;
        if($rs <= $numDate)
        {
            return true;
        }
        else
            return false;
    }
}


